//
//  HomeMoreItemController.m
//  SleepingElf
//
//  Created by karong on 2023/10/20.
//

#import "HomeMoreItemController.h"
#import "MoreItemRequest.h"
#import "HomeMoreItemCell.h"

@interface HomeMoreItemController ()
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) MoreItemRequest * request;
@end

@implementation HomeMoreItemController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.clearColor;
    MoreItemRequest * request = [MoreItemRequest new];
    request.classify_id = self.classify_id;
    self.request = request;
}

- (NSArray *)randomMusic {
    NSMutableSet *picks = [NSMutableSet new];
    if (self.dataArray.count > 0) {
        do {
            MusicModel * music = self.dataArray[arc4random() % self.dataArray.count];
            BOOL select = NO;
            if ([self.delegate respondsToSelector:@selector(homeMoreItemControllerMusicIsSelect:)]) {
                select = [self.delegate homeMoreItemControllerMusicIsSelect:music];
            }
            if (!select) {
                [picks addObject:music];
            }
            
        } while (picks.count != 3);
    }
    return [picks allObjects];
}

- (void)reloadData {
    [self.listView reloadData];
}

- (BOOL)disableAutoBeginRefresh {
    return YES;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeMoreItemCell * homeMoreItemCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeMoreItemCell" forIndexPath:indexPath];
    BOOL select = NO;
    if ([self.delegate respondsToSelector:@selector(homeMoreItemControllerMusicIsSelect:)]) {
        select = [self.delegate homeMoreItemControllerMusicIsSelect:self.dataArray[indexPath.row]];
    }
    [homeMoreItemCell setDataWithObject:self.dataArray[indexPath.row] select:select];
    return homeMoreItemCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(homeMoreItemControllerDidSelectMusic:)]) {
        [self.delegate homeMoreItemControllerDidSelectMusic:self.dataArray[indexPath.row]];
    }
    [collectionView reloadData];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = floor((SEScreenWidth - 20 - 20 - 29 - 29 - 29) / 4.0f);
    size.height = size.width + 30;
    return size;
}


@end
